﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Aspose.Pdf.Operators;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using XKPlatform.Common;
using XKPlatform.DataModels;

namespace XKPlatform.Controllers
{

    public class BaseController : Controller
    {
        public readonly SysUser userInfo;
        protected readonly IHttpContextAccessor _ctx;

        public BaseController(IHttpContextAccessor httpContext)
        {
            _ctx = httpContext;

            userInfo = SSOClient.getUser(_ctx.HttpContext);
            userInfo.project = "BB8A9356-E812-46A8-ABFA-8D3956F7ACF3";
        }
        //public BaseController()
        //{

        //}
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public (bool, string) DataValid(object obj)
        {
            var validationResults = new List<ValidationResult>();
            var validationContext = new ValidationContext(obj, null, null);

            //返回是否验证成功

            bool isValid = Validator.TryValidateObject(obj, validationContext, validationResults, true);

            if (!isValid)
            {
                string errorMsg = "";
                foreach (var item in validationResults)
                {
                    errorMsg += item.ErrorMessage + "\n";
                }
                return (false, errorMsg);
            }
            return (isValid, "");
        }

    }
}